import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/hidden_danger_control_page/danger_control_details_page/danger_control_details_controller.dart';
import 'package:url_launcher/url_launcher.dart';

/// 隐患治理详情
class DangerControlDetailsPage extends StatelessWidget {
  DangerControlDetailsPage({super.key});

  final controller = Get.put(DangerControlDetailsController());


  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '详情',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                ),
              ),
              leading: const BackButton(
                color: Colors.white,
              ),
              iconButtonColor: Colors.white,
              backgroundColor: const Color(0xFF5C6DFF),
            ),
            body: Container(
                color: const Color(0xFFF5F5F6),
                child: Stack(
                  children: [
                    // 产品基础信息
                    _getBasicInformation(),
                    Container(
                      margin: EdgeInsets.only(top: 85.w),
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      child: SingleChildScrollView(
                        child: Column(
                          children: [
                            // 产品详情
                            _getDetailsView(),
                            AppGaps.vGap14,
                            _getStorageCapacityView(),
                            AppGaps.vGap14,
                            // 整改说明
                            _rectification(),
                            AppGaps.vGap14,
                            // 验收情况
                            _acceptance(),
                            AppGaps.vGap20,
                          ],
                        ),
                      ),
                    ),
                  ],
                )),
          );
        });
  }
  /// 获取基础信息视图
  Widget _getBasicInformation() {
    return Container(
      height: 140.w,
      width: AppDimens.sh,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
      color: const Color(0xFF5C6DFF),
      child: Column(
        children: [
          Row(
            children: [
              SvgPicture.asset(
                AppImages.icon_title_product,
                width: 17.w,
                height: 17.w,
              ),
              AppGaps.hGap6,
              Expanded(
                  child: Text(
                controller.infoEntity.troubleshootContent!,
                style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.white,
                  overflow: TextOverflow.ellipsis,
                  fontWeight: FontWeight.w500,
                ),
              )),
            ],
          ),
          AppGaps.vGap8,
          Row(
            children: [
              Expanded(
                child: Row(
                  children: [
                    Text(
                      '巡检描述：',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 13.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    Expanded(
                        child: Text(
                      '${controller.infoEntity.dangerRecordDesc}',
                      style: TextStyle(
                        overflow: TextOverflow.ellipsis,
                        fontSize: 12.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    )),
                  ],
                ),
              ),
              Container(
                height: 14.w,
                width: 2.w,
                margin: EdgeInsets.only(right: 20.w, left: 10.w),
                decoration: const BoxDecoration(
                    gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                  Color(0x00ffffff),
                  Color(0xffffffff),
                  Color(0x00ffffff),
                ])),
              ),
              _getExpandButton()
            ],
          )
        ],
      ),
    );
  }

  /// 获取展开按钮
  Widget _getExpandButton() {
    return Row(
      children: [
        Text(
          controller.isExpanded ? '收起' : '展开',
          style: TextStyle(
            fontSize: 14.sp,
            color: Colors.white,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap6,
        SvgPicture.asset(
          controller.isExpanded ? AppImages.icon_circle_up : AppImages.icon_circle_down,
          width: 16.w,
          height: 16.w,
        )
      ],
    ).clickOnce(controller.onExpanded);
  }

  /// 获取详情视图
  Widget _getDetailsView() {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '拍照:',
              style: TextStyle(
                fontSize: 13.sp,
                color: AppColors.font_color_gray,
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: controller.infoEntity.dangerImages!.map<Widget>((item) {
                  return Container(
                    width: 74.w,
                    height: 74.w,
                    margin: EdgeInsets.only(bottom: 10.w),
                    decoration: BoxDecoration(
                      color: const Color(0xffF7F7FB),
                      border: Border.all(
                        color: const Color(0xffEBEDF0),
                      ),
                      borderRadius: BorderRadius.circular(8.w),
                      image: DecorationImage(
                        image: NetworkImage(Interface.fileUrl + item),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ).clickShowLarge(url: Interface.fileUrl + item);
                }).toList()),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '视频:',
              style: TextStyle(
                fontSize: 13.sp,
                color: AppColors.font_color_gray,
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: controller.infoEntity.dangerVideo!.map<Widget>((item) {
                  return Container(
                    width: 160.w,
                    height: 90.w,
                    margin: const EdgeInsets.only(bottom: 10),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: const Color(0xff060B15),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: IconButton(
                      icon: const Icon(
                        Icons.play_circle,
                        color: Colors.white,
                        size: 30,
                      ),
                      onPressed: () {
                        FileUtils.openFile(Interface.fileUrl + item);
                      },
                    ),
                  );
                }).toList()),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '语音:',
              style: TextStyle(
                fontSize: 13.sp,
                color: AppColors.font_color_gray,
              ),
            ),
          ),
          AppGaps.vGap8,
          controller.infoEntity.dangerVoice!.isNotEmpty
              ? CustomAudioplayers(
                  audioUrl: controller.infoEntity.dangerVoice!,
                  isEnabled: false,
                )
              : Container(),
          AppGaps.vGap8,
          Row(
            children: [
              _getFieldView('隐患上报人：', controller.infoEntity.registrant!),
              _getFieldView('电话：', controller.infoEntity.registrantUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患确认人：', controller.infoEntity.confirmBy!),
              _getFieldView('电话：', controller.infoEntity.confirmUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患整改人：', controller.infoEntity.liablePerson!),
              _getFieldView('电话：', controller.infoEntity.liableUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患验收人：', controller.infoEntity.checkAcceptPerson!),
              _getFieldView('电话：', controller.infoEntity.checkAcceptUserPhone!),
            ],
          ),
          Visibility(
              visible: controller.isExpanded,
              child: Column(
                children: [
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析对象：', controller.infoEntity.riskObjectName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('责任部门：', controller.infoEntity.hazardDep!),
                      _getFieldView('责任人：', controller.infoEntity.hazardLiablePerson!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析单元：', controller.infoEntity.riskUnitName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险事件：', controller.infoEntity.riskEventName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getCurrentRiskLevel(),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险描述：', controller.infoEntity.riskDescription!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('初始风险后果：', '${controller.infoEntity.initialRiskConsequences}'),
                      _getFieldView('初始风险度：', '${controller.infoEntity.initialRiskDegree}'),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getInitialRiskLevel(),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('管控措施：', '${controller.infoEntity.controlMeasures}'),
                    ],
                  ),
                ],
              ))
        ],
      ),
    );
  }

  /// 字段显示
  Widget _getFieldView(String title, String value, {Color? rightColor}) {
    return Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: const Color(0xFFA5A6B5),
              fontWeight: FontWeight.w400,
            ),
          ),
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: rightColor ?? const Color(0xFF495061),
              fontWeight: FontWeight.w400,
            ),
          ))
        ],
      ),
    );
  }

  /// 初始风险等级
  Widget _getInitialRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.initialRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('初始风险等级：', name, rightColor: rightColor);
  }

  /// 当前风险等级
  Widget _getCurrentRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.currentRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('当前风险等级：', name, rightColor: rightColor);
  }

  /// 获取隐患名称
  Widget _getStorageCapacityView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Container(
                width: 2.w,
                height: 12.w,
                color: const Color(0xff5C6DFF),
              ),
              AppGaps.hGap4,
              Expanded(
                child: Text(
                  '隐患名称：${controller.infoEntity.dangerName}',
                  style: TextStyle(
                    fontSize: 15.sp,
                    color: const Color(0xff262F38),
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          AppGaps.vGap14,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getHiddenDangerLevel(),
              _getFieldView('隐患类型：', controller.infoEntity.hazardDangerType!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('隐患来源：', controller.infoEntity.dangerSrc!),
              _getFieldView('治理类型：', controller.infoEntity.dangerManageType!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('巡检描述：', controller.infoEntity.dangerDesc!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('原因分析：', controller.infoEntity.dangerReason!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('控制措施：', controller.infoEntity.riskMeasureDesc!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('资金：', controller.infoEntity.cost!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('隐患整改期限：', controller.infoEntity.dangerManageDeadline!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _getFieldView('整改责任人：', controller.infoEntity.liablePerson!),
              _getFieldView('验收人：', controller.infoEntity.checkAcceptPerson!),
            ],
          ),
        ],
      ),
    );
  }

  /// 初始隐患等级
  Widget _getHiddenDangerLevel() {
    Color? rightColor;
    switch (controller.infoEntity.dangerLevel) {
      case '重大隐患':
        rightColor = const Color(0xffFD6B6B);
        break;
      default:
        rightColor = const Color(0xffFFCE24);
        break;
    }
    return _getFieldView('隐患等级：', controller.infoEntity.dangerLevel!, rightColor: rightColor);
  }

  /// 整改说明
  Widget _rectification() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Container(
                width: 2.w,
                height: 12.w,
                color: const Color(0xff5C6DFF),
              ),
              AppGaps.hGap4,
              Expanded(
                child: Text(
                  '整改说明：${controller.infoEntity.liableDesc}',
                  style: TextStyle(
                    fontSize: 15.sp,
                    color: const Color(0xff262F38),
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          AppGaps.vGap14,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '拍照:',
              style: TextStyle(
                fontSize: 13.sp,
                color: AppColors.font_color_gray,
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: controller.infoEntity.liableImages!.map<Widget>((item) {
                  return Container(
                    width: 74.w,
                    height: 74.w,
                    margin: EdgeInsets.only(bottom: 10.w),
                    decoration: BoxDecoration(
                      color: const Color(0xffF7F7FB),
                      border: Border.all(
                        color: const Color(0xffEBEDF0),
                      ),
                      borderRadius: BorderRadius.circular(8.w),
                      image: DecorationImage(
                        image: NetworkImage(Interface.fileUrl + item),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ).clickShowLarge(url: Interface.fileUrl + item);
                }).toList()),
          ),
        ],
      ),
    );
  }

  /// 验收情况
  Widget _acceptance() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Container(
                width: 2.w,
                height: 12.w,
                color: const Color(0xff5C6DFF),
              ),
              AppGaps.hGap4,
              Expanded(
                child: Text(
                  '验收情况：${controller.infoEntity.checkAcceptComment}',
                  style: TextStyle(
                    fontSize: 15.sp,
                    color: const Color(0xff262F38),
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          AppGaps.vGap14,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '拍照:',
              style: TextStyle(
                fontSize: 13.sp,
                color: AppColors.font_color_gray,
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: controller.infoEntity.checkAcceptImages!.map<Widget>((item) {
                  return Container(
                    width: 74.w,
                    height: 74.w,
                    margin: EdgeInsets.only(bottom: 10.w),
                    decoration: BoxDecoration(
                      color: const Color(0xffF7F7FB),
                      border: Border.all(
                        color: const Color(0xffEBEDF0),
                      ),
                      borderRadius: BorderRadius.circular(8.w),
                      image: DecorationImage(
                        image: NetworkImage(Interface.fileUrl + item),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ).clickShowLarge(url: Interface.fileUrl + item);
                }).toList()),
          )
        ],
      ),
    );
  }

}
